﻿using AppDataStruct;
using EECMISWS.WebProcessArgs;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace EECMISWS
{
    public class WebDataAccess
    {
        #region static field and functions

        private static object _lock = new object();
        protected static WebDataAccess _instance;
        public static WebDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new WebDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion

        private string _error = "";
        public string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        private string _appName = Assembly.GetExecutingAssembly().GetName().Name;
        public string InterfaceName
        {
            get
            {
                return _appName;
            }
        }

        public int TimeoutMillisecond
        {
            get;
            set;
        }

        public int ID
        {
            set;
            get;
        }

        public bool Inited
        {
            get;
            private set;
        }

        public bool Closed
        {
            get;
            private set;
        }

        public bool Enabled
        {
            get
            {
                return _option.Enabled;
            }
        }

        public bool UploadSXQM
        {
            get
            {
                return _option.UploadSXQM;
            }
        }

        private WSClientOption _option = new WSClientOption();

        private WSClient _webClient = null;

        private CodeMapService _mapService = null;

        private WebDataAccess()
        {
            Inited = false;
            Closed = false;
            LoadSettings();
        }

        private void LoadSettings()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            else
            {
                _option.Load();
            }
            _mapService = CodeMapService.Instance;
            if (!_mapService.LoadConfig())
            {
                _error = _mapService.Error;
                Log.WriteAsync(_error);
            }
        }

        public bool Login()
        {
            try
            {
                _webClient = new WSClient(_option.URL);
                _webClient.XTMC = _option.XTMC;
                _webClient.LoginName = _option.User;
                _webClient.PassWord = _option.GetPassword();
            }
            catch (Exception ex)
            {
                _error = "创建eecmiswsPortTypeClient发生了异常: " + ex.Message;
                Log.WriteAsync(_error);
                Log.WriteException(ex.ToString());
                return false;
            }
            if (_webClient.Login())
            {
                Inited = true;
                return true;
            }
            else
            {
                _error = _webClient.Message;
                return false;
            }
        }

        public void Logout()
        {
            if (!Inited)
            {
                return;
            }
            Closed = true;
            Log.AddEndingBoundary();
        }

        public bool Post(string param, out string outParam)
        {
            _error = "";
            outParam = "";
            try
            {
                UploadPhotoArgs paras = JsonHandler.ConvertToJsonObject<UploadPhotoArgs>(param);
                if (!paras.IsVaild())
                {
                    _error = "无效的上传参数: " + paras.ToString();
                    Log.WriteAsync(_error);
                    return false;
                }
                if (_webClient.SavePaperPhoto(paras.DestPhotoPath, paras.PhotoName))
                {
                    outParam = string.Format("照片{0}上传成功.", paras.PhotoName);
                    return true;
                }
                else
                {
                    _error = _webClient.Message;
                    outParam = string.Format("照片{0}上传失败:{1}", paras.PhotoName, _error);
                    return false;
                }
            }
            catch (Exception ex)
            {
                _error = "WebAisino上传发生了异常, 异常详情: " + ex.Message;
                Log.WriteAsync(_error);
                Log.WriteException(ex.ToString());
                return false;
            }
        }

        public PeopleQueryResult QueryPeopleInfo(string inParams)
        {
            Log.WriteAsync("QueryPeople in: ");
            PeopleQueryResult result = new PeopleQueryResult();
            QueryPeopleArgs paras = JsonHandler.ConvertToJsonObject<QueryPeopleArgs>(inParams);
            if (!paras.IsVaild())
            {
                result.ErrorMessage = _error = "无效的查询参数: " + paras.ToString();
                Log.WriteAsync(_error);
                return result;
            }
            string sfzh = paras.SFZH;
            _webClient.QueryPeople(sfzh, out result);
            Log.WriteAsync("QueryPeople return: " + result.Success.ToString());
            return result;
            //if () //查人口库
            //{

            //    QGRKInfo qgrkInfo = result.PersonInfo;
            //    appInfo.XB.Code = qgrkInfo.XB;
            //    appInfo.XB.Value = qgrkInfo.XB == "1" ? "男" : "女";
            //    appInfo.YJDZ = qgrkInfo.ZZXZ;
            //    appInfo.MZ.Code = qgrkInfo.MZ_CODE;
            //    appInfo.MZ.Value = qgrkInfo.MZ;
            //    appInfo.ZWXM = qgrkInfo.XM;
            //    appInfo.CSRQ = qgrkInfo.CSRQ;
            //    appInfo.BSWS = qgrkInfo.SNSW;

            //    string csd = qgrkInfo.CSD;
            //    int index = csd.IndexOf("户口所在地");
            //    string left = csd.Substring(0, index).Replace(" ", "");
            //    string[] leftArr = left.Split('-');
            //    appInfo.CSD.Code = leftArr[0];
            //    appInfo.CSD.Value = leftArr[1];

            //    string right = csd.Substring(index);
            //    index = right.IndexOf('-');
            //    appInfo.HKSZD.Code = right.Substring(index - 6, 6);
            //    appInfo.HKSZD.Value = right.Substring(index + 1);
            //}

        }

        /// <summary>
        /// 查询是否满足申请条件
        /// </summary>
        /// <param name="inParams"></param>
        /// <returns></returns>
        public PeopleCheckResult CheckPersonInfo(string sfzh, string xm, string xb, string csrq)
        {
            PeopleCheckResult result = new PeopleCheckResult();

            bool isSpecialList = false;
            //控制对象核查 
            if (_webClient.QueryCheckSpecialList(sfzh, ref isSpecialList))
            {
                Log.WriteAsync("CheckSpecialList return: true");
                result.CheckSpecialListResult = isSpecialList ? CheckResult.Yes : CheckResult.No;
            }
            else
            {
                result.CheckSpecialListResult = CheckResult.Failed;
                result.SetError(_webClient.Message);
            }

            result.CheckOfficalResult = CheckResult.Yes;
            //bool isOfficial = false;
            //if (_webClient.QueryOfficial(sfzh, xm, xb, csrq, ref isOfficial))
            //{
            //    Log.WriteAsync("QueryOfficial return: true");
            //    result.Offical = _webClient.Official;
            //    result.CheckOfficalResult = isOfficial ? CheckResult.Yes : CheckResult.No;
            //}
            //else
            //{
            //    result.CheckOfficalResult = CheckResult.Failed;
            //    result.SetError(_webClient.Message);
            //}
            return result;
        }

        /// <summary>
        /// 查询办证申请信息
        /// </summary>
        /// <param name="inParams"></param>
        /// <param name="appInfo"></param>
        /// <returns></returns>
        public bool QueryAppointment(string sfzh, ref PreApplyInfo preAppInfo, out bool hasData)
        {
            hasData = false;
            AppointmentInfo aptInfo = new AppointmentInfo();
            if (_webClient.QueryAppointment(sfzh, out aptInfo))
            {
                Log.WriteAsync("QueryApply return: true");
                if (aptInfo.IsEmpty)
                {
                    hasData = false;
                    Log.WriteAsync("办证申请信息为空.");
                    return true;
                }
                hasData = true;

                AppointmentApplyInfo sqttInfo = aptInfo.ApplyInfos[0];
                preAppInfo.LXDH = sqttInfo.lxdh;
                preAppInfo.JJLXR = sqttInfo.jjqklxr;
                preAppInfo.JJLXDH = sqttInfo.jjqklxrdh;
                preAppInfo.YJDZ = sqttInfo.emsdz;
                preAppInfo.YB = sqttInfo.yzbm;
                preAppInfo.SJR = sqttInfo.sjr;
                preAppInfo.SJRLXDH = sqttInfo.sjhm;
                if (!string.IsNullOrEmpty(sqttInfo.zy))
                {
                    string zyName = _mapService["ZY", sqttInfo.zy];
                    preAppInfo.ZY = new CodeMap(sqttInfo.zy, zyName);
                }
                if (sqttInfo.sfxtkzd == "0")
                {
                    preAppInfo.QZFS.Code = "01";
                }
                else if (sqttInfo.sfxtkzd == "1")
                {
                    preAppInfo.QZFS.Code = "02";
                }
                else
                {
                    preAppInfo.QZFS.Code = "01";
                }

                if (aptInfo.ContainsSQ_HZ)
                {
                    AppointmentApplyInfo sqInfo = aptInfo.HZ;
                    if (sqInfo != null)
                    {
                        preAppInfo.HZSQ.YWLB.Code = sqInfo.bzlb;
                        preAppInfo.HZSQ.YWBH = sqInfo.ywbh;
                        preAppInfo.HZSQ.SFJZ.Code = "0";
                        preAppInfo.HZSQ.SFJZ.Value = "否";
                        if (!string.IsNullOrEmpty(sqInfo.qwd))
                        {
                            string qwdName = _mapService["QWD", sqttInfo.qwd];
                            preAppInfo.HZSQ.QWD = new CodeMap(sqttInfo.qwd, qwdName);
                        }
                        if (!string.IsNullOrEmpty(sqInfo.cjsy))
                        {
                            string cjsyName = _mapService["CJSY", sqttInfo.cjsy];
                            preAppInfo.HZSQ.SQSY = new CodeMap(sqttInfo.cjsy, cjsyName);
                        }
                    }
                }
                if (aptInfo.ContainsSQ_GAZ)
                {
                    AppointmentApplyInfo sqInfo = aptInfo.GAZ;
                    if (sqInfo != null)
                    {
                        List<LabelApplyInfo> listLabels = new List<LabelApplyInfo>();
                        preAppInfo.SCZSQ.YWLB.Code = sqInfo.bzlb;
                        preAppInfo.SCZSQ.YWBH = sqInfo.ywbh;
                        if (sqInfo.sfxqz == "1")
                        {
                            preAppInfo.SCZSQ.SFQZ.Code = "1";
                            preAppInfo.SCZSQ.SFQZ.Value = "是";
                            List<AppointmentQZInfo> list = aptInfo.SCZQZ;
                            foreach (AppointmentQZInfo qzInfo in list)
                            {
                                LabelApplyInfo labelInfo = new LabelApplyInfo();
                                labelInfo.QWD.Code = qzInfo.qwd;
                                labelInfo.QZZL.Code = qzInfo.qzzl;
                                labelInfo.YXCS.Code = qzInfo.qzyxcs;
                                labelInfo.YXQX.Code = qzInfo.GetYXQXCode();
                                listLabels.Add(labelInfo);
                            }
                            if (aptInfo.GXRInfo != null)
                            {
                                preAppInfo.SCZSQ.QSXM = aptInfo.GXRInfo.zwxm;
                                preAppInfo.SCZSQ.QSXB.Code = aptInfo.GXRInfo.xb;
                                preAppInfo.SCZSQ.QSXB.Value = _mapService["XB", aptInfo.GXRInfo.xb];
                                preAppInfo.SCZSQ.QSSFZH = aptInfo.GXRInfo.sfzhm;
                                preAppInfo.SCZSQ.QSGX.Code = aptInfo.GXRInfo.qsgx;
                                preAppInfo.SCZSQ.QSGX.Value = _mapService["QSGX", aptInfo.GXRInfo.qsgx];
                                preAppInfo.SCZSQ.LXZJHM = aptInfo.GXRInfo.zjhm;
                            }
                        }
                        preAppInfo.SCZSQ.SCZQZ = listLabels;
                    }
                }
                if (aptInfo.ContainsSQ_TWZ)
                {
                    AppointmentApplyInfo sqInfo = aptInfo.TWZ;
                    if (sqInfo != null)
                    {
                        preAppInfo.DLZSQ.YWLB.Code = sqInfo.bzlb;
                        preAppInfo.DLZSQ.YWBH = sqInfo.ywbh;
                        if (sqInfo.sfxqz == "1")
                        {
                            preAppInfo.DLZSQ.SFQZ.Code = "1";
                            preAppInfo.DLZSQ.SFQZ.Value = "是";
                            AppointmentQZInfo qzInfo = aptInfo.TWN_QZ;
                            if (qzInfo != null)
                            {
                                preAppInfo.DLZSQ.QZZL.Code = qzInfo.qzzl;
                                preAppInfo.DLZSQ.YXCS.Code = qzInfo.qzyxcs;
                                preAppInfo.DLZSQ.YXQX.Code = qzInfo.GetYXQXCode();
                            }
                            else
                            {
                                Log.WriteAsync("糟糕，台湾签注信息为空.");
                            }
                        }
                    }
                }
                preAppInfo.SaveAsXml();
                return true;
            }
            else
            {
                _error = _webClient.Message;
                Log.WriteAsync("QueryApply return: false.");
                return false;
            }
        }

        /// <summary>
        /// 查询办证申请信息
        /// </summary>
        /// <param name="inParams"></param>
        /// <param name="appInfo"></param>
        /// <returns></returns>
        public bool QueryApplyInfo(string sfzh, out PreApplyInfo appInfo)
        {
            appInfo = new PreApplyInfo();
            //PreApplyInfo appInfoTemp = new PreApplyInfo();
            if (_webClient.QueryApply(sfzh, ref appInfo))
            {
                Log.WriteAsync("QueryApply return: true");
                //appInfo.LXDH = appInfoTemp.LXDH;
                //appInfo.JJLXR = appInfoTemp.JJLXR;
                //appInfo.JJLXDH = appInfoTemp.JJLXDH;
                //appInfo.YJDZ = appInfoTemp.YJDZ;
                //appInfo.YB = appInfoTemp.YB;
                //appInfo.SJR = appInfoTemp.SJR;
                //appInfo.SJRLXDH = appInfoTemp.SJRLXDH;
                appInfo.SaveAsXml();
                return true;
            }
            else
            {
                _error = _webClient.Message;
                Log.WriteAsync("QueryApply return: false.");
                return false;
            }
        }

        public string MakeYWBH()
        {
            string ywbh = "";
            // 获取业务编号
            if (_webClient.MakeBarCode(_option.Ywbh_Prefix, out ywbh))
            {
                Log.WriteAsync("MakeBarCode return: true");
                return ywbh;
            }
            else
            {
                _error = _webClient.Message;
                Log.WriteAsync("MakeBarCode return: false. Reason: " + _error);
                return ywbh;
            }
        }

        /// <summary>
        /// 查询证件信息(护照，港澳证，大陆证)
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>
        public QueryPaperInfo QueryPaperInfo(string sfzh)
        {
            try
            {
                bool b0 = false;
                bool b1 = false;
                bool b2 = false;

                QueryPaperInfo papers = new QueryPaperInfo();

                PaperInfo hzInfo = null;

                //查询护照办证信息
                if (_webClient.QueryPaper(sfzh, "HZ", out hzInfo))
                {
                    Log.WriteAsync("QueryPaper HZ return: true");
                    papers.HZ = hzInfo;
                    b0 = true;
                }
                else
                {
                    Log.WriteAsync("QueryPaper HZ return: false");
                    papers.SetError(_webClient.Message);
                }

                PaperInfo sczInfo = null;
                if (_webClient.QueryPaper(sfzh, "SCZ", out sczInfo))
                {
                    Log.WriteAsync("QueryPaper SCZ return: true");
                    papers.SCZ = sczInfo;
                    b1 = true;
                }
                else
                {
                    Log.WriteAsync("QueryPaper SCZ return: false");
                    papers.SetError(_webClient.Message);
                }

                PaperInfo dlzInfo = null;
                if (_webClient.QueryPaper(sfzh, "DLZ", out dlzInfo))
                {
                    Log.WriteAsync("QueryPaper DLZ return: true");
                    papers.DLZ = dlzInfo;
                    b2 = true;
                }
                else
                {
                    Log.WriteAsync("QueryPaper DLZ return: false");
                    papers.SetError(_webClient.Message);
                }
                papers.Success = b0 && b1 && b2;
                if (!papers.Success)
                {
                    Log.WriteAsync("继续查询全国证件库");
                    var qg_pager = QueryQGPaperInfo(sfzh);
                    Log.WriteAsync($"省级证件库返回:{JsonHandler.ConvertToJsonString(papers)}");
                    Log.WriteAsync($"全国证件库返回:{JsonHandler.ConvertToJsonString(qg_pager)}");
                    if (qg_pager.Success)
                    {
                        if (qg_pager.HZ != null)
                        {
                            if (papers.HZ == null || string.IsNullOrEmpty(papers.HZ.qfrq))
                            {
                                papers.HZ = qg_pager.HZ;
                                Log.WriteAsync($"使用全国库的HZ数据1：{papers.HZ.zjhm},qfrq{papers.HZ.qfjg}");
                            }
                            else
                            {
                                if (int.Parse(papers.HZ.qfrq) < int.Parse(qg_pager.HZ.qfrq))
                                {
                                    papers.HZ = qg_pager.HZ;
                                    Log.WriteAsync($"使用全国库的HZ数据2：{papers.HZ.zjhm},qfrq{papers.HZ.qfjg}");
                                }
                            }
                        }
                        else
                        {
                            Log.WriteAsync($"全国库没查到护照信息");
                        }

                        if (qg_pager.SCZ != null)
                        {
                            if (papers.SCZ == null || string.IsNullOrEmpty(papers.SCZ.qfrq))
                            {
                                papers.SCZ = qg_pager.SCZ;
                                Log.WriteAsync($"使用全国库的SCZ数据1：{papers.SCZ.zjhm},qfrq{papers.SCZ.qfjg}");
                            }
                            else
                            {
                                if (int.Parse(papers.SCZ.qfrq) < int.Parse(qg_pager.SCZ.qfrq))
                                {
                                    papers.SCZ = qg_pager.SCZ;
                                    Log.WriteAsync($"使用全国库的SCZ数据2：{papers.SCZ.zjhm},qfrq{papers.SCZ.qfjg}");
                                }
                            }
                        }
                        else
                        {
                            Log.WriteAsync($"全国库没查到港澳证信息");
                        }
                        if (qg_pager.DLZ != null)
                        {
                            if (papers.DLZ == null || string.IsNullOrEmpty(papers.DLZ.qfrq))
                            {
                                papers.DLZ = qg_pager.DLZ;
                                Log.WriteAsync($"使用全国库的DLZ数据1：{papers.DLZ.zjhm},qfrq{papers.DLZ.qfjg}");
                            }
                            else
                            {
                                if (int.Parse(papers.DLZ.qfrq) < int.Parse(qg_pager.DLZ.qfrq))
                                {
                                    papers.DLZ = qg_pager.DLZ;
                                    Log.WriteAsync($"使用全国库的DLZ数据2：{papers.DLZ.zjhm},qfrq{papers.DLZ.qfjg}");
                                }
                            }
                        }
                        else
                        {
                            Log.WriteAsync($"全国库没查到台湾证信息");
                        }

                        Log.WriteAsync("查询全国证件库完成");
                    }
                    else
                    {
                        Log.WriteAsync("查询全国证件库失败");
                    }
                }
                Log.WriteAsync($"最终查询证件HZ:[{papers?.HZ?.zjhm}]-[{papers?.HZ?.zjyxqz}],SCZ:[{papers?.SCZ?.zjhm}]-[{papers?.SCZ?.zjyxqz}],DLZ:[{papers?.DLZ?.zjhm}]-[{papers?.DLZ?.zjyxqz}]");

                return papers;
            }
            catch (Exception ex)
            {
                Log.WriteException("查询证件信息异常:" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 查询证件信息(护照，港澳证，大陆证)
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>
        public QueryPaperInfo QueryQGPaperInfo(string sfzh)
        {
            QueryPaperInfo papers = new QueryPaperInfo();
            List<CHPaperInfo> listInfos = null;
            //查询护照办证信息
            if (_webClient.QueryQGPaper(sfzh, out listInfos))
            {
                papers.Success = true;

                for (int i = 0; i < listInfos.Count; i++)
                {
                    PaperInfo info = listInfos[i].ToPaperInfo();
                    try
                    {
                        if (info.zjzl == "14")
                        {
                            //如果号码已经赋值了，比对证件签发时间，取最后签发的那个证件
                            if (papers.HZ != null && !string.IsNullOrEmpty(papers.HZ.zjhm) && papers.HZ != null && !string.IsNullOrEmpty(papers.HZ.qfrq))
                            {
                                if (int.Parse(info.qfrq) < int.Parse(papers.HZ.qfrq))
                                {
                                    continue;
                                }
                            }
                            papers.HZ = info;
                        }
                        else if (info.zjzl == "25")
                        {
                            //如果号码已经赋值了，比对证件签发时间，取最后签发的那个证件
                            if (papers.DLZ != null && !string.IsNullOrEmpty(papers.DLZ.zjhm) && papers.DLZ != null && !string.IsNullOrEmpty(papers.DLZ.qfrq))
                            {
                                if (int.Parse(info.qfrq) < int.Parse(papers.DLZ.qfrq))
                                {
                                    continue;
                                }
                            }
                            papers.DLZ = info;
                        }
                        else if (info.zjzl == "21")
                        {
                            //如果号码已经赋值了，比对证件签发时间，取最后签发的那个证件
                            if (papers.SCZ != null && string.IsNullOrEmpty(papers.SCZ.zjhm) && papers.SCZ != null && !string.IsNullOrEmpty(papers.SCZ.qfrq))
                            {
                                if (int.Parse(info.qfrq) < int.Parse(papers.SCZ.qfrq))
                                {
                                    continue;
                                }
                            }
                            papers.SCZ = info;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteException("QueryQGPaperInfo exception:" + ex.ToString());
                    }
                }
                Log.WriteAsync("QueryQGPaperInfo return: true");
            }
            else
            {
                papers.Success = false;
                Log.WriteAsync("QueryPaper HZ return: false");
                papers.SetError(_webClient.Message);
            }
            return papers;
        }

        /// <summary>
        ///  查询个人信息
        /// </summary>
        /// <param name="param"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public bool QueryAllInfo(string inParams, out PreApplyInfo appInfo)
        {
            string xmlPath = "";
            appInfo = new PreApplyInfo();
            _error = "";
            try
            {
                QueryPeopleArgs paras = JsonHandler.ConvertToJsonObject<QueryPeopleArgs>(inParams);
                if (!paras.IsVaild())
                {
                    _error = "无效的查询参数: " + paras.ToString();
                    Log.WriteAsync(_error);
                    return false;
                }
                string sfzh = paras.SFZH;

                PeopleQueryResult result = null;
                if (_webClient.QueryPeople(sfzh, out result)) //查人口库
                {
                    Log.WriteAsync("QueryPeople return: true");
                    QGRKInfo qgrkInfo = result.PersonInfo;
                    appInfo.XB.Code = qgrkInfo.XB;
                    appInfo.XB.Value = qgrkInfo.XB == "1" ? "男" : "女";
                    appInfo.YJDZ = qgrkInfo.ZZXZ;
                    appInfo.MZ.Code = qgrkInfo.MZ_CODE;
                    appInfo.MZ.Value = qgrkInfo.MZ;
                    appInfo.ZWXM = qgrkInfo.XM;
                    appInfo.CSRQ = qgrkInfo.CSRQ;
                    appInfo.BSWS = qgrkInfo.SNSW;

                    string csd = qgrkInfo.CSD;
                    int index = csd.IndexOf("户口所在地");
                    string left = csd.Substring(0, index).Replace(" ", "");
                    string[] leftArr = left.Split('-');
                    appInfo.CSD.Code = leftArr[0];
                    appInfo.CSD.Value = leftArr[1];

                    string right = csd.Substring(index);
                    index = right.IndexOf('-');
                    appInfo.HKSZD.Code = right.Substring(index - 6, 6);
                    appInfo.HKSZD.Value = right.Substring(index + 1);
                }

                bool isSpecialList = false;

                //控制对象核查  1：受控   0：否
                //if (_webClient.QueryCheckSpecialList(sfzh, ref isSpecialList))
                //{
                //    Log.WriteAsync("CheckSpecialList return: true");
                //    appInfo.KZDX = isSpecialList ? "1" : "0";
                //}

                //bool isOfficial = false;
                //if (_webClient.QueryOfficial(sfzh, ref isOfficial))
                //{
                //    Log.WriteAsync("QueryOfficial return: true");
                //    OfficialInfo offInfo = _webClient.Official;
                //    appInfo.GJGZRYDW = offInfo.RSZGDW;  //国家工作人员单位 不为空则是国家工作人员
                //}

                PaperInfo paperInfo = null;
                //查询护照办证信息
                if (_webClient.QueryPaper(sfzh, "HZ", out paperInfo))
                {
                    Log.WriteAsync("QueryPaper HZ return: true");
                    appInfo.HZSQ.ZJHM = paperInfo.zjhm;
                    appInfo.HZSQ.ZJYXQZ = paperInfo.zjyxqz;
                    appInfo.HZSQ.PaperStatus = paperInfo.zjzt;
                }
                if (_webClient.QueryPaper(sfzh, "SCZ", out paperInfo))
                {
                    Log.WriteAsync("QueryPaper SCZ return: true");
                    appInfo.SCZSQ.ZJHM = paperInfo.zjhm;
                    appInfo.SCZSQ.ZJYXQZ = paperInfo.zjyxqz;
                    appInfo.SCZSQ.PaperStatus = paperInfo.zjzt;
                }
                if (_webClient.QueryPaper(sfzh, "DLZ", out paperInfo))
                {
                    Log.WriteAsync("QueryPaper DLZ return: true");
                    appInfo.DLZSQ.ZJHM = paperInfo.zjhm;
                    appInfo.DLZSQ.ZJYXQZ = paperInfo.zjyxqz;
                    appInfo.DLZSQ.PaperStatus = paperInfo.zjzt;
                }

                PreApplyInfo appInfoTemp = new PreApplyInfo();
                if (_webClient.QueryApply(sfzh, ref appInfoTemp))
                {
                    Log.WriteAsync("QueryApply return: true");
                    appInfo.LXDH = appInfoTemp.LXDH;
                    appInfo.JJLXR = appInfoTemp.JJLXR;
                    appInfo.JJLXDH = appInfoTemp.JJLXDH;
                    appInfo.YJDZ = appInfoTemp.YJDZ;
                    appInfo.YB = appInfoTemp.YB;
                    appInfo.SJR = appInfoTemp.SJR;
                    appInfo.SJRLXDH = appInfoTemp.SJRLXDH;
                }
                appInfo.SaveAsXml();
                return true;
            }
            catch (Exception ex)
            {
                _error = "WebAisino上传发生了异常, 异常详情: " + ex.Message;
                Log.WriteAsync(_error);
                Log.WriteException(ex.ToString());
                return false;
            }
        }

        public bool SavePreApply(PreApplyInfo appInfo)
        {
            try
            {
                if (!_webClient.SavePreApply(appInfo))
                {
                    _error = _webClient.Message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                _error = "保存申请信息发生了异常:" + ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 上传手写前面（现在部分地方已经不开此接口，通过生物特征上传）
        /// </summary>
        /// <param name="appInfo"></param>
        /// <returns></returns>
        public bool SaveHandWriting(PreApplyInfo appInfo)
        {
            if (string.IsNullOrEmpty(appInfo.HZSQ.YWBH))
            {
                return true;
            }
            if (!_webClient.SaveHandWriting(appInfo.HZSQ.YWBH, appInfo.SXQM_ZIP))
            {
                _error = _webClient.Message;
                return false;
            }
            return true;
        }

        public bool SaveLiveportrait(PreApplyInfo appInfo)
        {
            if (!string.IsNullOrEmpty(appInfo.HZSQ.YWBH))
            {
                if (!_webClient.SaveLiveportrait(appInfo.HZSQ.YWBH, appInfo.XCRX))
                {
                    _error = _webClient.Message;
                    return false;
                }
            }
            if (!string.IsNullOrEmpty(appInfo.SCZSQ.YWBH))
            {
                if (!_webClient.SaveLiveportrait(appInfo.SCZSQ.YWBH, appInfo.XCRX))
                {
                    _error = _webClient.Message;
                    return false;
                }
            }
            if (!string.IsNullOrEmpty(appInfo.DLZSQ.YWBH))
            {
                if (!_webClient.SaveLiveportrait(appInfo.DLZSQ.YWBH, appInfo.XCRX))
                {
                    _error = _webClient.Message;
                    return false;
                }
            }
            return true;
        }

        public bool SaveSFZHKBPdf(PreApplyInfo appInfo)
        {

            if (!_webClient.SaveSFZHKBPDF(appInfo))
            {
                _error = _webClient.Message;
                return false;
            }

            return true;
        }

        public bool SaveFingerPrint(PreApplyInfo appInfo)
        {
            if (!_webClient.SaveFingerPrint(appInfo))
            {
                _error = _webClient.Message;
                return false;
            }

            return true;
        }
    }
}
